//
//  ZMZSTianJiaViewController.m
//  ZhangMiZhuShou
//
//  Created by ryou on 2021/7/26.
//

#define kpmTitleArray @[@"图标(可选)",@"账号",@"密码",@"标记"]
#import "ZMZSTianJiaViewController.h"

@interface ZMZSTianJiaViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property(nonatomic,strong)UITableView *ZMZSZAcountMTableView;
@property(nonatomic,strong)UIImageView *ZMZSZAcountMcameraImageView;
@property(nonatomic,strong)UITextField *ZMZSZtextField1;
@property(nonatomic,strong)UITextField *ZMZSZtextField2;
@property(nonatomic,strong)UITextField *ZMZSZtextField3;


@end

@implementation ZMZSTianJiaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加";
    [self.view addSubview:self.ZMZSZAcountMTableView];

}

-(UITableView *)ZMZSZAcountMTableView{
    
    if (!_ZMZSZAcountMTableView) {
        _ZMZSZAcountMTableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _ZMZSZAcountMTableView.delegate=self;
        _ZMZSZAcountMTableView.dataSource=self;
        _ZMZSZAcountMTableView.backgroundColor=kZMXZSMainColor;
        _ZMZSZAcountMTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
    }
    return _ZMZSZAcountMTableView;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    UITableViewCell *ZMZSAMCell=[tableView cellForRowAtIndexPath:indexPath];
    if (!ZMZSAMCell) {
        ZMZSAMCell=[[UITableViewCell alloc]init];
    }
   
    [self createViewWithCell:ZMZSAMCell p:indexPath];
    return ZMZSAMCell;
}
-(void)createViewWithCell:(UITableViewCell *)ZMZSAMCell p:(NSIndexPath *)indexPath{
    //    351 × 122 pixels
    ZMZSAMCell.backgroundColor=[UIColor clearColor];
    CGFloat zmxzsbgW = kZMXZSWidth - 40;
    CGFloat zmxzsbgH = zmxzsbgW * 74 / 355;
    ZMZSAMCell.selectionStyle=UITableViewCellSelectionStyleNone;
    UIImageView * ZMZScellBg=[[UIImageView alloc]initWithFrame:CGRectMake(20, 10, zmxzsbgW,zmxzsbgH)];
    ZMZScellBg.image=[UIImage imageNamed:@"zmxzs_icon_cellbg.png"];
    ZMZScellBg.contentMode=UIViewContentModeScaleToFill;
    [ZMZSAMCell.contentView addSubview:ZMZScellBg];
    ZMZScellBg.userInteractionEnabled=YES;
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(30, 0, 100, zmxzsbgH)];
    title.text=kpmTitleArray[indexPath.row];
    title.textColor=[UIColor grayColor];
    [ZMZScellBg addSubview:title];
    
    if (indexPath.row == 0) {
        UIImageView *ZMZSZAcountMcameraImageView=[[UIImageView alloc]initWithFrame:CGRectMake(zmxzsbgW - zmxzsbgH - 15, 15, zmxzsbgH -30, zmxzsbgH - 30)];
        ZMZSZAcountMcameraImageView.image = [UIImage imageNamed:@"zmxzs_icon_xiangji.png"];
        [ZMZScellBg addSubview: ZMZSZAcountMcameraImageView];
        ZMZSZAcountMcameraImageView.layer.cornerRadius=10;
        ZMZSZAcountMcameraImageView.clipsToBounds=YES;
        ZMZSZAcountMcameraImageView.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ZMZSZAcountMcameraImageViewClick)];
        [ZMZSZAcountMcameraImageView addGestureRecognizer:tap];
        _ZMZSZAcountMcameraImageView=ZMZSZAcountMcameraImageView;
    }
    else
    {
        UITextField *textfield=[[UITextField alloc]initWithFrame:CGRectMake(1, 0, zmxzsbgW - 40, zmxzsbgH)];
        textfield.textAlignment = NSTextAlignmentRight;
        textfield.returnKeyType = UIReturnKeyDone;
        textfield.delegate=self;
        textfield.textColor =[UIColor grayColor];
        [ZMZScellBg addSubview:textfield];
        
        NSString *holderText = @"请输入";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName
                                value:[UIColor grayColor]
                                range:NSMakeRange(0, holderText.length)];
        textfield.attributedPlaceholder = placeholder;

        if (indexPath.row==1) {
            _ZMZSZtextField1 = textfield;
        }
        if (indexPath.row == 2) {
            _ZMZSZtextField2 = textfield;
        }
        if (indexPath.row ==3) {
            _ZMZSZtextField3 = textfield;
        }
    }
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return kpmTitleArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat zmxzsbgW = kZMXZSWidth - 40;
    CGFloat zmxzsbgH = zmxzsbgW * 74 / 355;
    
    return zmxzsbgH + 20;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    CGFloat zmxzsbgW = kZMXZSWidth - 40;
    CGFloat zmxzsbgH = zmxzsbgW * 74 / 355;
    
    UIView *foodview=[[UIView alloc]init];
    
    UIButton *zmxzsAcountMsavebtn=[[UIButton alloc]initWithFrame:CGRectMake(kZMXZSWidth / 2 - 70, (zmxzsbgH + 20 )/2 - 30, 140, 60)];
    [zmxzsAcountMsavebtn setBackgroundImage:[UIImage imageNamed:@"zmxzs_btn_bg.png"] forState:UIControlStateNormal];
    [foodview addSubview:zmxzsAcountMsavebtn];
    zmxzsAcountMsavebtn.layer.cornerRadius = 30;
    zmxzsAcountMsavebtn.clipsToBounds=YES;
    [zmxzsAcountMsavebtn setTitle:@"保存" forState:UIControlStateNormal];
    [zmxzsAcountMsavebtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [zmxzsAcountMsavebtn addTarget:self action:@selector(zmxzsAcountMsavebtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    return foodview;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    CGFloat zmxzsbgW = kZMXZSWidth - 40;
    CGFloat zmxzsbgH = zmxzsbgW * 74 / 355;
    return zmxzsbgH + 20;
}
-(void)zmxzsAcountMsavebtnClick{
    
    [self pmsave];
}
-(void)pmsave{
    if (_ZMZSZtextField1.text.length>0&&_ZMZSZtextField2.text.length>0&&_ZMZSZtextField3.text.length>0) {
        
        NSUserDefaults *ZMZSAcountMuser=[NSUserDefaults standardUserDefaults];
        NSArray *data=[ZMZSAcountMuser valueForKey:@"ZMZSaccountmanagermuserdata"];
        NSMutableArray *ndata=[NSMutableArray arrayWithArray:data];
       
        NSData *imageData =UIImagePNGRepresentation(_ZMZSZAcountMcameraImageView.image);
        NSDictionary *dict=@{@"ZMZSZMZSaccountmanagermuserdatazhanghao":_ZMZSZtextField1.text,
                             @"ZMZSZMZSaccountmanagermuserdatamima":_ZMZSZtextField2.text,
                             @"ZMZSaccountmanagermuserpmbiaoji":_ZMZSZtextField3.text,
                             @"ZMZSZMZSaccountmanagermuserpmbiaojipmicon":imageData };
        [ndata addObject:dict];
        [ZMZSAcountMuser setValue:ndata forKey:@"ZMZSaccountmanagermuserdata"];
        UIAlertController *pmalert=[UIAlertController alertControllerWithTitle:@"保存成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action=[UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }];
        [pmalert addAction:action];
        [self presentViewController:pmalert animated:YES completion:nil];

    }else{
        UIAlertController *pmalert=[UIAlertController alertControllerWithTitle:@"请填写完整" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action=[UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [pmalert addAction:action];
        [self presentViewController:pmalert animated:YES completion:nil];
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.view endEditing:YES];
    return YES;
}
-(void)ZMZSZAcountMcameraImageViewClick{
    UIImagePickerController *vc = [UIImagePickerController new];
        //有三种 照相机 文件夹 相册
       // vc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        vc.delegate = self;
        vc.allowsEditing = YES;
        [self presentViewController:vc animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    NSLog(@"%@",info);
    UIImage *image = info[@"UIImagePickerControllerOriginalImage"];
    _ZMZSZAcountMcameraImageView.image=image;
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

